package com.sixth.controller.wx;

import com.sixth.bean.MarketAddress;
import com.sixth.bean.MarketCoupon;
import com.sixth.bean.MarketUser;
import com.sixth.bean.bo.WXCartAddBO;
import com.sixth.bean.bo.WXOrderSubmitBo;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.bo.WXCartCheckedBO;
import com.sixth.bean.vo.WXCartCheckoutCheckedAddressVO;
import com.sixth.bean.vo.WXCartCheckoutVO;
import com.sixth.bean.vo.WXCartIndexVO;
import com.sixth.bean.bo.WXCartUpdateBO;
import com.sixth.exception.CustomValidationException;
import com.sixth.service.CartService;
import com.sixth.service.CouponService;
import com.sixth.service.WxAddressService;
import com.sixth.util.MarketInfoUtil;
import com.sixth.util.ValidationUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.attribute.GroupPrincipal;
import java.util.List;
import java.util.Map;

/**
 * @Author lixinhang
 * @Time 2022/9/11 12:53
 */
@RestController
@RequestMapping("wx/cart")
public class WXCartController {

    @Autowired
    CartService cartService;
    @Autowired
    WxAddressService wxAddressService;
    @Autowired
    CouponService couponService;

    //点进商品详情，手机底部会显示该用户购物车中商品的数量
    @RequestMapping("goodscount")
    public BaseRespVo wxCartGoodscount(){
        //获取当前登录的用户id
        MarketUser marketUser = MarketInfoUtil.getMarketUser();
        //如果未登录状态，则购物车商品数量显示0
        if(marketUser == null){
            return BaseRespVo.ok(0);
        }
        Integer id = marketUser.getId();
        //查询该用户购物车中的商品数量
        Integer goodscount = cartService.wxCartGoodscount(id);
        return BaseRespVo.ok(goodscount);
    }

    //购物车中添加商品
    @RequestMapping("add")
    public BaseRespVo wxCartAdd(@RequestBody WXCartAddBO wxCartAddBO){
        //判断是否是登录状态
        MarketUser marketUser = MarketInfoUtil.getMarketUser();
        //如果没有登录，则返回{"errno":501,"errmsg":"请登录"}
        if(marketUser == null){
            return BaseRespVo.invalidNoAndMsg("请登录",501);
        }
        Integer id = marketUser.getId();
        Integer code = cartService.wxCartAdd(wxCartAddBO,id);   //如果添加成功，则返回当前购物车中的商品数量，如果没有添加成功，则返回null

        //如果code=200,则代表加入成功，如果code=404,则代表库存不足，加入购物车失败
        if(code == null){
            return BaseRespVo.invalidNoAndMsg("库存不足",711);
        }else{
            return BaseRespVo.ok(code);
        }
    }

    //查看购物车中的商品
    @RequestMapping("index")
    public BaseRespVo wxCartIndex(){
        //获取user_id
        MarketUser marketUser = MarketInfoUtil.getMarketUser();
        if(marketUser == null){
            return BaseRespVo.ok("未登录");
        }
        Integer userId = marketUser.getId();
        //去数据库中查询cart表，user_id为${user_id}、deleted为false的商品
        WXCartIndexVO wxCartIndexVO = cartService.wxCartIndex(userId);

        return BaseRespVo.ok(wxCartIndexVO);
    }

    //在购物车中，选中或者取消选中某商品 isChecked=1代表选中，isChecked=0表示取消选中
    //把wxCartCheckedBO中list所包含的product_id指定的记录，修改为isChecked所指定的状态
    @RequestMapping("checked")
    public BaseRespVo wxCartChecked(@RequestBody WXCartCheckedBO wxCartCheckedBO){
        //获取一下user_id
        Integer userId = MarketInfoUtil.getMarketUser().getId();
        //将wxCartCheckedBO中list所包含的product_id指定的记录，并且user_id为userId,deleted未删除的记录，checked字段修改为wxCartCheckedBO中所包含的isChecked
        cartService.wxCartChecked(wxCartCheckedBO,userId);
        //重新去查询该用户购物车中的信息
        WXCartIndexVO wxCartIndexVO = cartService.wxCartIndex(userId);
        return BaseRespVo.ok(wxCartIndexVO);
    }

    //在购物车中，更新所选商品的数量
    @RequestMapping("update")
    public BaseRespVo wxCartUpdate(@RequestBody @Valid WXCartUpdateBO wxCartUpdateBO, BindingResult bindingResult) throws CustomValidationException {
        //先校验一下参数，number的值不能超过1000000
        ValidationUtils.valid(bindingResult);

        //获取一下user_id
        MarketUser marketUser = MarketInfoUtil.getMarketUser();
        Integer userId = marketUser.getId();

        Integer code = cartService.wxCartUpdate(wxCartUpdateBO);
        if(code == 200){
            return BaseRespVo.ok();
        }else{
            return BaseRespVo.error("库存不足");
        }
    }

    //在购物车中，删除选中的商品列表
    @RequestMapping("delete")
    public BaseRespVo wxCartDelete(@RequestBody Map map){
        Integer userId = MarketInfoUtil.getMarketUser().getId();
        //获取productId的列表
        List<Integer> productIds = (List<Integer>)(map.get("productIds"));
        cartService.wxCartDelete(productIds,userId);
        WXCartIndexVO wxCartIndexVO = cartService.wxCartIndex(userId);
        return BaseRespVo.ok(wxCartIndexVO);
    }

    //todo 购物车中,或者商品界面,下单的方法,前端会判断用户是否登录
    @RequestMapping("checkout")
    public BaseRespVo wxCartCheckout(WXOrderSubmitBo wxOrderSubmitBo){
        Integer userId = MarketInfoUtil.getMarketUser().getId();

        WXCartCheckoutVO wxCartCheckoutVO  = cartService.wxCartCheckout(wxOrderSubmitBo,userId);

        return BaseRespVo.ok(wxCartCheckoutVO);
    }

    //向购物车中快速地添加一条商品记录
    @RequestMapping("fastadd")
    public BaseRespVo wxCartFastadd(@RequestBody WXCartAddBO wxCartAddBO){
        //无需校验是否登录状态，前端会去判断
        Integer userId = MarketInfoUtil.getMarketUser().getId();
        Integer cartId = cartService.wxCartFastadd(wxCartAddBO,userId);
        if (cartId == null) {
            return BaseRespVo.invalidNoAndMsg("库存不足",711);
        }
        return BaseRespVo.ok(cartId);
    }
}
